package com.tanhua.server.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class MqMessageService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 统一发送log消息
     * @param type 业务类型
     * 0101为登录，0102为注册，
     * 0201为发动态，0202为浏览动态，0203为动态点赞，0204为动态喜欢，0205为评论，0206为动态取消点赞，0207为动态取消喜欢，
     * 0301为发小视频，0302为小视频点赞，0303为小视频取消点赞，0304为小视频评论
     * @param key routingKey
     * @param busId business ID, 业务对象的id, 如：对动态点赞，busId=动态的id
     */
    public void sendLogMsg(String type,String key,String busId){
        // 登录用户id
        Long loginUserId = UserHolder.getUserId();
        // 构建消息内容
        Map<String,Object> msgMap = new HashMap<>();
        msgMap.put("userId", loginUserId);
        msgMap.put("type", type);
        msgMap.put("busId", busId);
        msgMap.put("logTime", DateUtil.today());
        String msg = JSON.toJSONString(msgMap);
        // 发送给mq
        rabbitTemplate.convertAndSend("tanhua.log.exchange", "log."+key,msg);
    }


}
